import 'package:abc/controller/watch_history_controller.dart';
import 'package:abc/view/CustomAppBar.dart';
import 'package:abc/view/common_video_cell.dart';
import 'package:abc/view/pagination_list_view.dart';
import 'package:abc/view/video_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class WatchHistoryPage extends GetView<WatchHistoryController> {
  const WatchHistoryPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF0E0F26), // 深色背景
      appBar: CustomAppBar(
        title: "观看历史",
        onBack: () {
          Get.back();
        },
      ),
      body: PaginationListView<VideoModel>(
        controller: controller,
        itemBuilder: _buildVideoItem,
        viewType: PaginationViewType.gridView,
        gridConfig: const PaginationGridConfig(
          crossAxisCount: 2,
          crossAxisSpacing: 12.0,
          mainAxisSpacing: 12.0,
          mainAxisExtent: 238.0,
        ),
        pageSize: 20,
        enableRefresh: true,
        padding: const EdgeInsets.only(top: 6, bottom: 10, left: 16, right: 16),
      ),
    );
  }
  
  // 构建视频项
  Widget _buildVideoItem(BuildContext context, VideoModel video, int index) {
    // 第3行（索引为20, 21）显示1个item，其他行显示2个item
    // 每行2个item，所以第3行的索引范围是20-21
    if (index >= 20 && index <= 21) {
      // 对于第3行的item，我们使用一个包含两个视频项的容器
      // 但由于每行只能显示一个item，我们只显示当前视频项
      return Container(
        width: double.infinity, // 占满整行宽度
        child: CommonVideoCell(
          video: video,
          layoutType: CommonVideoCellType.AnimeLayout,
          layoutConfig: CommonVideoCellConfig(
            imageHeight: 219.0,
            imageWidth: 164.0,
            tagHeight: 17.0,
            textHeight: 18.5,
            radius: 8,
            tagMargin: 4,
          ),
          onTap: () {
            // 注意：由于项目中没有VideoDetailPage，这里暂时留空
            // Navigator.of(context).push(
            //   MaterialPageRoute(
            //     builder: (context) => VideoDetailPage(
            //       videoModel: video,
            //       menu: widget.menu,
            //     ),
            //   ),
            // );
          },
        ),
      );
    } else {
      // 其他行正常显示
      return CommonVideoCell(
        video: video,
        layoutType: CommonVideoCellType.AnimeLayout,
        layoutConfig: CommonVideoCellConfig(
          imageHeight: 219.0,
          imageWidth: 164.0,
          tagHeight: 17.0,
          textHeight: 18.5,
          radius: 8,
          tagMargin: 4,
        ),
        onTap: () {
          // 注意：由于项目中没有VideoDetailPage，这里暂时留空
          // Navigator.of(context).push(
          //   MaterialPageRoute(
          //     builder: (context) => VideoDetailPage(
          //       videoModel: video,
          //       menu: widget.menu,
          //     ),
          //   ),
          // );
        },
      );
    }
  }
}